//
//  RTHomeTableViewController.swift
//  Weibo
//
//  Created by Brisinga on 16/3/2.
//  Copyright © 2016年 Root. All rights reserved.
//

import UIKit

class RTHomeTableViewController: RTVisitorViewController {
    
    
    private let RTStatuesID = "status"
    
    
   private lazy var statusList:RTStatusListModelView = RTStatusListModelView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
       
        
        if !isLogin {
            
            visitorView.setVisitorViewInfo(nil, message: nil)
            
            return
        }
        
        setupUI()
        
        // 加载网络数据
        loadData()
        
        self.tableView .registerClass(RTStatusCell.self, forCellReuseIdentifier: RTStatuesID)
    }
    
    
    
    // Mark 请求网络数据
    @objc private func loadData() {
    
     statusList.loadData(pullView.isAnimating()) { (isSuccess) -> () in
        
        if isSuccess {
            
            self.tableView .reloadData()
        }
        
        // 停止刷新
        self.endRefresh()
        }
        
    }
    
    
    // 停止刷新数据
    private func endRefresh() {
    
        pullView.stopAnimating()
        
        refreshView.endRfreshing()
    }
    
    // 初始化内部组件
    private func setupUI() {
        
        
        tableView.addSubview(refreshView)
        
        tableView.backgroundColor = UIColor(white: 239/255, alpha: 1)
        
        tableView.separatorStyle = .None
        
        // 设置leftBarButtonItem
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageNamed: "navigationbar_friendsearch", target: nil, action: nil)
        
        // 设置
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageNamed: "navigationbar_pop", target:self, action: "pushViewController")
        
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 300
        
        refreshView.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
        
       
        
        tableView.tableFooterView = pullView
    }
   
    
    
    // push控制器
   @objc private func pushViewController() {
        
        let qtVC = RTQTCodeViewController()
        
        navigationController?.pushViewController(qtVC, animated: true)
    }
    
    
    
    // 懒加载
    private lazy var refreshView: RTRefreshControl = RTRefreshControl()
    
    
    // 网络指示器
    private lazy var pullView: UIActivityIndicatorView = {
    
        let pullView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        
//        pullView.color = UIColor.orangeColor()
        
        pullView.backgroundColor = UIColor.purpleColor()
        
//        pullView.startAnimating()
        return pullView
    }()
  
}


extension RTHomeTableViewController {

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusList.statuesViewModelArr?.count ?? 0
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(RTStatuesID, forIndexPath: indexPath) as! RTStatusCell
        
//        cell.textLabel?.text = statusList.statuesArr![indexPath.row].text
        cell.statusViewModel = statusList.statuesViewModelArr![indexPath.row]
        
        return cell
    }
    
    
    // s拉刷新
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.row == statusList.statuesViewModelArr!.count-1 && pullView.isAnimating() == false {
        
            self.pullView.startAnimating()
            print("wotmzhuanle")
            self.loadData()
        }
    }
}

